package com.cait.dutyfree.action.impl;

import java.util.Map;
import java.util.Properties;
import java.util.Random;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class PasswordRecoveryAction extends ActionSupport implements ServletContextAware,SessionAware, ServletRequestAware{
	
	private static final long serialVersionUID = -2004536492564753733L;
	private ServletContext servletContext;
	private HttpServletRequest request;
	private Map<String,Object> session;
	
	public String execute(){
	    	
	    	try{
	    		String sendTo = getRequest().getParameter("txtEmail");
	    		String validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	    		String psw = "";
	    		
	    		Random generator = new Random();
	    		for (int i=0; i<12; i++) {
	    		        psw+=validChars.charAt(generator.nextInt(validChars.length()));
	    		}
	    		String host = "smtp.gmail.com";
	    	    String from = "dfassnandun@gmail.com";
	    	    String pass = "dfass@123";
	    	    Properties props = System.getProperties();
	    	    props.put("mail.smtp.starttls.enable", "true");
	    	    props.put("mail.smtp.host", host);
	    	    props.put("mail.smtp.user", from);
	    	    props.put("mail.smtp.password", pass);
	    	    props.put("mail.smtp.port", "587");
	    	    props.put("mail.smtp.auth", "true");
	    	 
	    	    String[] to = {sendTo}; 
	    	 
	    	    Session session = Session.getDefaultInstance(props, null);
	    	    MimeMessage message = new MimeMessage(session);
	    	    message.setFrom(new InternetAddress(from));
	    	 
	    	    InternetAddress[] toAddress = new InternetAddress[to.length];
	    	 
	    	    // To get the array of addresses
	    	    for( int i=0; i < to.length; i++ ) { // changed from a while loop
	    	        toAddress[i] = new InternetAddress(to[i]);
	    	    }
	    	 
	    	    for( int i=0; i < toAddress.length; i++) { // changed from a while loop
	    	        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
	    	    }
	    	    
	    	    message.setSubject("DFASS - Password Recovery");
	    	    message.setText("Your password is reset to : \n\t"+psw);
	    	    Transport transport = session.getTransport("smtp");
	    	    transport.connect(host, from, pass);
	    	    transport.sendMessage(message, message.getAllRecipients());
	    	    transport.close();  
	    	    return SUCCESS;
	    	}catch (Exception e) {
	    		System.out.println(e);
	    		return NONE;
			}
	    }
	
	public void setServletContext(ServletContext context) {
		this.servletContext = context;
	}
	
	public ServletContext getServletContext() {
		return servletContext;
	}
	
	@SuppressWarnings("unchecked")
	public void setSession(@SuppressWarnings("rawtypes") Map session) {
		this.session = session;
	}
	
	public Map<String, Object> getSession() {
		return session;
	}
	
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	public HttpServletRequest getRequest() {
		return request;
	}
	
}
